<?php

namespace adminmatrix\plugin\command;

use adminmatrix\migration\Command;
use think\console\Input;
use think\console\Output;
use think\console\Table;

class PluginList extends Command
{
    protected function configure()
    {
        $this->setName("plugin:list")
            ->setDescription("查询插件列表信息");
    }

    protected function execute(Input $input, Output $output)
    {
        $path = $this->app->getRootPath() . "plugins";
        $count = 0;
        $installCount = 0;
        $uninstallCount = 0;
        $table = new Table();
        $table->setCellAlign(2);
        $table->setStyle("box-double");
        $table->setHeader(["插件标识" , "插件名称" , "版本号" , "插件描述" , "作者" , "安装状态" , "插件类型"]);
        foreach ( glob($path . "/*") as $paths)
        {
            $pathInfo = pathinfo($path . $paths);
            $config   = json_decode(file_get_contents(  $paths . "/config.json") , true);

            $status = $config["status"] ?? false;
            $count++;
            if ($status)
            {
                $installCount++;
            }else{
                $uninstallCount++;
            }
            $status = $config["status"] ? "已安装" :"未安装";
            $table->addRow([$pathInfo['filename'] , $config["name"] , $config["version"] , $config["description"] , $config["author"] , $status  , $config["type"]]);
        }


        echo $table->render();
        $output->writeln("当前插件共：{$count} ;已安装:{$installCount}; 未安装:{$uninstallCount}");

    }
}